<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\reward\Log as RewardLogModel;


use think\Db;

/**
 * 产品管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','user2','user3'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
            }
            

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function writeOff($ids=null)
    {
        $row = $this->model->get($ids);
        if(!$row)
        {
            $this->error(__('操作错误请重试'));
        }
        if($row['status'] != 0)
        {
            $this->error(__('订单非未出行订单'));
        }
        Db::startTrans();
        try{
            $data['status'] = 1;
            $data['handle_time'] = time();
            $status = $this->model->where('id',$ids)->update($data);
            if($status === false)
            {
                Db::rollback();
                $this->error('操作失败，请重试');
                
            }
            $r_where['order_id'] = $ids;
            $r_where['type'] = 1;
            $r_where['status'] = '1';
            $reward_list = RewardLogModel::where($r_where)->select();
            $UserModel = model('User');
            $user_statuss = true;
            if($reward_list)
            {
                foreach($reward_list as $k=>$list)
                {
                    $user_info = $UserModel->where('id',$list['agent_id'])->find();
                    if($user_info)
                    {
                        $user_data['money'] = $user_info['money'] + $list['money'];
                        $user_data['total_money'] = $user_info['total_money'] + $list['money'];
                        $user_status = $UserModel->where('id',$list['agent_id'])->update($user_data);
                        if($user_status === false)
                        {
                            $user_statuss = false;
                        }
                    }
                    $u_data['status'] = '2';
                    $status = RewardLogModel::where('id',$list['id'])->update($u_data);
                    if($status === false)
                    {
                        $user_statuss = false;
                    }
                }
            }
            if($user_statuss === false)
            {
                Db::rollback();
                $this->error('操作失败，请重试');
                
            }
            
            //获取佣金记录
        }catch(Expection $e)
        {
            Db::rollback();
            $this->error('操作失败，请重试');
               
        }
        Db::commit();
        $this->success('操作成功');
    }
    
    public function cancel($ids=null)
    {
        $row = $this->model->get($ids);
        if(!$row)
        {
            $this->error(__('操作错误请重试'));
        }
        if($row['status'] != 0)
        {
            $this->error(__('订单非未出行订单'));
        }
        Db::startTrans();
        try{
            $data['status'] = 2;
            $data['handle_time'] = time();
            $status = $this->model->where('id',$ids)->update($data);
            if($status === false)
            {
                Db::rollback();
                $this->error('操作失败，请重试');
                
            }
            $r_where['order_id'] = $ids;
            $r_where['type'] = 1;
            $r_where['status'] = '1';
            $reward_list = RewardLogModel::where($r_where)->select();
            $UserModel = model('User');
            $user_statuss = true;
            if($reward_list)
            {
                foreach($reward_list as $k=>$list)
                {
                    $status = RewardLogModel::where('id',$list['id'])->delete();
                    if($status === false)
                    {
                        $user_statuss = false;
                    }
                }
            }
            if($user_statuss === false)
            {
                Db::rollback();
                $this->error('操作失败，请重试');
                
            }
            
            //获取佣金记录
        }catch(Expection $e)
        {
            Db::rollback();
            $this->error('操作失败，请重试');
               
        }
        Db::commit();
        $this->success('操作成功');
    }
}
